<?php
#                                                                                 
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 
include_once("./root.inc.php");
include_once("${root}/mysql/database_info.php");
include_once("${root}/mysql/common.php");
include_once("${root}/common.php");

function emailDayBefore(){
	
	$sixDaysOld = date("Y-m-d", strtotime("-6 days"));
	
	$result = mysql_query("SELECT * FROM `needs` WHERE `postDate` = '".$sixDaysOld."'");
	
	if (mysql_num_rows($result) != 0){
	
		while ($peopleToEmail = mysql_fetch_array($result)){
			
			$message = "Tomorrow your post entitled \"".$peopleToEmail['title']."\" will be deleted off of the Needs Board, if you have not received any response, feel free to post it again.";
			
			if (mail($peopleToEmail['email'], "One day left on SHCC Needs Board.", $message, "From: webservant@southhillcalvary.org")){
				
				printf("Emailed: \"%s\" about their post \"%s\", scheduled to be deleted tomorrow.\n\n", $peopleToEmail['email'], $peopleToEmail['title']);
			}
			else{
				
				printf("!!!Email to: \"%s\" about their post \"%s\", scheduled to be deleted tomorrow, did not send!!!\n\n", $peopleToEmail['email'], $peopleToEmail['title']);
			}	
		}
	}
	else{
		
		printf("No people emailed today.\n\n");
	}
}

function deleteWeekOld(){
	
	$weekOld = date("Y-m-d", strtotime("-1 week"));
	
	$result = mysql_query("SELECT * FROM `needs` WHERE `postDate` = '".$weekOld."'");
	
	if (mysql_num_rows($result) != 0){
	
		while ($postsToDelete = mysql_fetch_array($result)){
		
			mysql_query("DELETE FROM `needs` WHERE `id`='".$postsToDelete['id']."'");
			
			printf("Post entitled \"%s\" deleted today.\n\n", $postsToDelete['title']);
		}
	}
	else{
		
		printf("No posts deleted today.\n\n");
	}
}

connect_select_test("needs");

printf("Today is : %s\n\n", date("Y-m-d"));

emailDayBefore();

deleteWeekOld();

?>